﻿
CREATE proc dbo.CreateCalculationFile
	@FileName	nvarchar(400), -- incl. full path,
	@SourceFile nvarchar(400) = null,
	@CopyState	bit = 0 output
as
begin
	declare @vorlage	nvarchar(400) = '\\TP-S01\server\Vorlagen\Ka\Ka15.xlsm',
			@command	nvarchar(4000) = 'COPY "';

	if @SourceFile is not null
	begin
		set @command += @SourceFile + '" "' + @FileName + '"';
	end else
	begin
		set @command += @vorlage + '" "' + @FileName + '"';
	end

	begin try
		--print @command;
		EXEC master.dbo.xp_cmdshell @command;
		
	end try
	begin catch
		print ERROR_MESSAGE();
		raiserror('Error on create a new calculation file.', 16,1)
	end catch
	set @CopyState = 1;
	return 0;
end